/**
 * interaction-callback.js
 * todo：收到comet推送过来的事件回调
 */


/**
 * 座席状态发生变化触发此函数（如Ready、NotReady）
 */
function onAgentStatusChanged(result) {
	var voice_state='';
	var chat_state='';
	 $.each(result, function() {
		 if(this.userState.mediaType=='voice'){
			 voice_state=this.userState.state ;			
		 }else{
			 chat_state=this.userState.state; 
		 }
		 
	 });
	// if(result.mediaType=="voice") {
	//var agentStatus = result.state;
	if (voice_state == "LoggedOut"||chat_state=="LoggedOut") {
		showButtons(6);
		window.parent.postMessage(result, '*');
	} else if (voice_state == "LoggedIn"||chat_state=="LoggedIn") {
		 showButtons(2);
		 window.parent.postMessage(result, '*');
	} else if (voice_state == "NotReady") {
		window.parent.postMessage(result, '*');
		if ($("#btnReady").hasClass("disable")&&$("#btnNotReady").hasClass("disable")) {
			return;
		}
		showButtons(4);
	} else if (voice_state == "Ready") {
		window.parent.postMessage(result, '*');
		if ($("#btnReady").hasClass("disable")&&$("#btnNotReady").hasClass("disable")) {
			return;
		}
		showButtons(3);
	} else if (voice_state == "ACW"||chat_state=="ACW") {
		window.parent.postMessage(result, '*');
		showButtons(5);
	}else{
		///alert("座席状态改变失败！");
	}
	// changeUI(state);
}

/**
 * onCallStateChangeMessage 拨打电话状态改变
 * 
 * @param result
 * @return
 */
function onCallStateChangeMessage(result) {
	var callState = result.call.state;
	console.log(connId + "-dai1-" + transferConnId);
	if (callState == "Dialing") {
		if (result.call.callType == "Consult") {// 咨询第三方
			transferConnId = result.call.connId;
			showButtons(14);
		} else {
			showButtons(7);
			connId = result.call.connId;
		}
		console.log(connId + "-dia2-" + transferConnId);
	} else if (callState == "Established") {
		console.log(connId + "-Established1-" + transferConnId);
		if (result.call.callType == "Consult") {
			if (transferConnId == "") {
				showButtons(17);
			} else {
				if (result.call.userData
						&& result.call.userData.ecloud_consult_operation == "transfer") {
					showButtons(20);// 咨询转接应答中
				} else {
					showButtons(18);// 咨询会议应答中
					//transferConnId=="";
				}
			}
			console.log(connId + "-Established2-" + transferConnId);
		} else {
			if (result.messageName == "EventPartyAdded") {//发起会议方，会议接起
				transferConnId = "";
				showButtons(19);
			} else if (result.messageName == "EventPartyDeleted") {
				transferConnId = "";
				showButtons(9);
			} else if (result.messageName == "EventPartyChanged") {//接受会议，会议接起
				if (result.thirdPartyRole == "ConferencedBy") {
					showButtons(19);
				} else if (result.thirdPartyRole == "TransferredBy") {
					showButtons(9);
				}
				connId = result.call.connId;
			} else if (result.messageName == "EventEstablished"
					&& result.thirdPartyRole == "ConferencedBy") {
				showButtons(19);
			} else if (result.messageName == "EventRetrieved") {
				if (result.call.userData
						&& result.call.userData.ecloud_retriveType == "retriveType") {
					showButtons(9);
				} else {
					showButtons(11);
				}
			} else {
				showButtons(9);
				// 使用html5的postMessage解决跨域问题
				//window.parent.postMessage(result, '*');
				// window.parent.popScreen(result);
			}
		}
		console.log(connId + "-Established3-" + transferConnId);
		window.parent.postMessage(result, '*');
	} else if (callState == 'Retrieve') {
		showButtons(11);
	} else if (callState == "Held") {
		showButtons(10);
	} else if (callState == 'Released') {
		console.log(connId + "-Released1-" + transferConnId);
		if (result.call.callType == "Consult") {
			if (transferConnId == "") {
				showButtons(13);
			} else {
				if (result.call.userData
						&& (result.call.userData.ecloud_consult_operation == "transfer"||result.call.userData.ecloud_consult_operation == "conference")) {
					var prams = "{\"ecloud_retriveType\":\"retriveType\"}";
					// 咨询转接未被接起
					retrieveCall(connId);
					updateUserDataVoice(connId, prams);
					transferConnId = "";
					//接起
					showButtons(9);
				} else if(result.call.userData
						&& result.call.userData.es_satisfaction == "1"){
					//转满意度完成
					console.log(connId + "-released2-" + transferConnId);
					transferConnId = "";
					showButtons(13);//ecloud_consult_operation
				} else if (result.call.userData
						&& (result.call.userData.ecloud_consult_operation == "conference_complete"||result.call.userData.ecloud_consult_operation == "transfer_complete")) {
					//转接或会议完成
					transferConnId = "";
					//接起
					showButtons(13);
				} else {
					console.log(connId + "-released2-" + transferConnId);
					retrieveCall(connId);
					// 会议完成
					transferConnId = "";
					showButtons(9);
				}//
				console.log(connId + "-Released3-" + transferConnId);
			}
		} else {
			showButtons(13);
		}
		window.parent.postMessage(result, '*');
	} else if (callState == 'Ringing') {
		showButtons(8);
		if (result.phoneNumber) {
			var phoneNo = result.phoneNumber;
			$("#phoneNo").val(phoneNo);
		} else {
			$("#phoneNo").val("unknown");
		}
		connId = result.call.connId;
		window.parent.postMessage(result, '*');
	}
}

/**
 * opencti处理异常触发此函数
 * 
 * @param result
 * @return
 */
function onCallMessageError(result) {
	showErrorMsg(result);
}

/**
 * opencti错误处理详细提示
 * @param result
 * @return
 */
function showErrorMsg(result) {
	// 35:Client does not own the interaction
	var message = "";
	if (result.errorCode == 40) {
		message = "没有更多可用的Licenses";
	} else if (result.errorCode == 41) {
		message = "话机异常，请检查换机";
	} else if (result.errorCode == 42) {
		message = "资源已回收";
	} else if (result.errorCode == 43) {
		message = "";
	} else if (result.errorCode == 50) {
		message = "未知的错误代码, 请求不能处理";
	} else if (result.errorCode == 51) {
		message = "该座席未上线";
	} else if (result.errorCode == 52) {
		message = "内部错误";
	} else if (result.errorCode == 53) {
		message = "请求参数是无效的";
	} else if (result.errorCode == 54) {
		message = "没有连接到交换机";
	} else if (result.errorCode == 55) {
		message = "协议版本错误";
	} else if (result.errorCode == 56) {
		message = "";
	} else if (result.errorCode == 57) {
		message = "话机异常，请检查话机";
	} else if (result.errorCode == 58) {
		message = "话机异常，请检查话机";
	} else if (result.errorCode == 59) {
		message = "未在配置库中配置该分机";
	} else if (result.errorCode == 61) {
		message = "请求中的分机无效";
	} else if (result.errorCode == 71) {
		message = "无效的号码";
	} else if (result.errorCode == 93) {
		message = "目标状态无效";
	} else if (result.errorCode == 96) {
		message = "不能添加一个新的呼叫到会议中";
	} else if (result.errorCode == 415) {
		message = "目标号码无效";
	} else if (result.errorCode == 527) {
		message = "该工号已登录";
	} else if (result.errorCode == 565) {
		message = "无效的状态";
	} else if (result.errorCode == 1008) {
		message = "拒绝邀请";
	} else if (result.errorCode == 1019) {
		message = "会话无应答";
	} else if (result.errorCode == 1141) {
		message = "请求的对象不兼容";
	} else if (result.errorCode == 1161) {
		message = "";
	} else if (result.errorCode == 10101) {
		message = "与Genesys Server连接失败";
	} else if (result.errorCode == 10102) {
		message = "目标号码为空或无效";
	} else if (result.errorCode == 10103) {
		message = "通话未开始或已结束";
	} else if (result.errorCode == 10104) {
		message = "座席签入失败";
	} else if (result.errorCode == 10105) {
		message = "座席签出失败";
	} else if (result.errorCode == 10106) {
		message = "座席状态改变失败";
	} else if (result.errorCode == 10107) {
		message = "电话呼叫失败";
	} else if (result.errorCode == 10108) {
		message = "电话接听失败";
	} else if (result.errorCode == 10109) {
		message = "电话挂断失败";
	} else if (result.errorCode == 10110) {
		message = "通话保持失败";
	} else if (result.errorCode == 10111) {
		message = "通话取回失败";
	} else if (result.errorCode == 10112) {
		message = "两步转接发起失败";
	} else if (result.errorCode == 10113) {
		message = "两步转接完成失败";
	} else if (result.errorCode == 10114) {
		message = "两步会议发起失败";
	} else if (result.errorCode == 10115) {
		message = "两步会议完成失败";
	} else if (result.errorCode == 10116) {
		message = "单步转接失败";
	} else if (result.errorCode == 10117) {
		message = "电话盲转失败";
	} else if (result.errorCode == 10118) {
		message = "单步会议失败";
	} else if (result.errorCode == 10119) {
		message = "将会议一方置为耳聋模式失败";
	} else if (result.errorCode == 10120) {
		message = "将会议一方取消耳聋模式失败";
	} else if (result.errorCode == 10121) {
		message = "添加随路数据失败";
	} else if (result.errorCode == 10122) {
		message = "删除随路数据失败";
	} else if (result.errorCode == 10123) {
		message = "更新随路数据失败";
	} else if (result.errorCode == 10124) {
		message = "打开静音失败";
	} else if (result.errorCode == 10125) {
		message = "关闭静音失败";
	} else if (result.errorCode == 10126) {
		message = "发送DTMF失败";
	} else if (result.errorCode == 10127) {
		message = "发送用户事件失败";
	} else if (result.errorCode == 10128) {
		message = "监听电话失败";
	} else if (result.errorCode == 10129) {
		message = "取消监听电话失败";
	} else if (result.errorCode == 10130) {
		message = "将会议一方踢出会议失败";
	} else if (result.errorCode == 8199) {// No such session exists
		// stop();
	} else if (result.errorCode == 8200) {// User already in the session
		// stop();
	} else {
		if(result.errorCode!=''&&result.errorMessage){
			message=result.errorMessage;	
		}else{
		    message = "";
		}
	}
	if (message != "") {// 会话已经结束
		alert(message);
	}
}

function scrollMessage(obj) {
	try {
		var div = document.getElementById(obj);
		div.scrollTop = div.scrollHeight;
	} catch (e) {
	}

}
